<?php
if (!defined("DAO_VOCLASS_PATH")) {
    die("no define DAO_VOCLASS_PATH");
}
abstract class DBConfig {
    const TOKEN_KEY_PRE = "MASTER_SLAVE_DATABASE_TOKEN_";
    private static $_tokenTime = 5;
    private static $_dbConfigData;
    private static $_existsToken;
    private static $_tokenHelper;
    private static $_customDAOConfig = array();
    public static function getDBConfig($key, $ms = "MASTER") {
        if ($ms == "MASTER") {
            return self::$_dbConfigData[$key]["MASTER"];
        } elseif ($ms == "SLAVE") {
            $slaveCount = count(self::$_dbConfigData[$key]["SLAVE"]);
            $slaveIndex = self::getSlaveIndex($slaveCount);
            return self::$_dbConfigData[$key]["SLAVE"][$slaveIndex];
        } else {
            die("Die in db config ms. ");
        }
    }
    public static function setTokenHelper(ITokenHelper $tokenHelper) {
        self::$_tokenHelper = $tokenHelper;
    }
    public static function getSlaveIndex($slaveCount) {
        $slaveIndex = time();
        return $slaveIndex % $slaveCount;
    }
    public static function setTokenTimeConfig($second) {
        self::$_tokenTime = $second;
    }
    public static function setToken($key) {
        if (!(self::$_tokenHelper instanceof ITokenHelper)) {
            echo "ITokenHelper config error in " . __FILE__ . ":" . __LINE__;
            exit;
        }
        $tokenKey = self::TOKEN_KEY_PRE . $key;
        self::$_tokenHelper->set($tokenKey, time());
    }
    public static function existsToken($key) {
        if (!(self::$_tokenHelper instanceof ITokenHelper)) {
            echo "ITokenHelper config error in " . __FILE__ . ":" . __LINE__;
            exit;
        }
        if (self::$_existsToken === true) {
            return true;
        }
        if (!is_array(self::$_dbConfigData[$key]["SLAVE"]) || count(self::$_dbConfigData[$key]["SLAVE"]) <= 0) {
            self::$_existsToken = true;
            return true;
        }
        $tokenKey = self::TOKEN_KEY_PRE . $key;
        $timeTime = self::$_tokenHelper->get($tokenKey);
        //var_dump($timeTime);
        if (!$timeTime) {
            return false;
        }
        //var_dump(time() - $timeTime);
        if (time() - $timeTime > self::$_tokenTime) {
            return false;
        }
        self::$_existsToken = true;
        return true;
    }
    public static function setDBConfig($key, $dbHost, $dbUser, $dbPsw, $dbName) {
        self::$_dbConfigData[$key]["MASTER"] = array(
            "DB_HOST" => $dbHost
            ,"DB_USER" => $dbUser
            ,"DB_PSW" => $dbPsw
            ,"DB_NAME" => $dbName
        );
    }
    public static function addSlaveDBConfig($key, $dbHost, $dbUser, $dbPsw, $dbName) {
        self::$_dbConfigData[$key];
        if (isset(self::$_dbConfigData[$key]) && is_array(self::$_dbConfigData[$key])) {
            $dbConfig = array(
                "DB_HOST" => $dbHost
                ,"DB_USER" => $dbUser
                ,"DB_PSW" => $dbPsw
                ,"DB_NAME" => $dbName
            );
        }
        self::$_dbConfigData[$key]["SLAVE"][] = $dbConfig;
    }
    public static function setDAOConfig($table, $className) {
        self::$_customDAOConfig[$table] = $className;
        //var_dump(self::$_customDAOConfig);
    }
    public static function getDAOClass($table) {
        //var_dump($table);
        if (strlen(self::$_customDAOConfig[$table]) <= 0) {
            return null;
        }
        //var_dump(self::$_customDAOConfig[$table]);
        $nameParts = explode(".", self::$_customDAOConfig[$table]);
		$className = end($nameParts);
        $classPath = implode('/', $nameParts);
		$classPath = $classPath.".class.php";
        //var_dump($nameParts);
        if(!class_exists($className, false)){
			require_once $classPath;
		}
        return $className;
    }
}